'use strict';

/* Controllers */
app.controller('fundCtrl', ['$scope', '$http', '$state', 'apiService', '$cookieStore', '$timeout', 'toaster', '$rootScope', '$window', function ($scope, $http, $state, apiService, $cookieStore, $timeout, toaster, $rootScope, $window) {

    $scope.recharge = {
        accountId: $rootScope.userObj['id']
    };


    // 入金
    $scope.rechargeFun = function () {
        $scope.btn_disabled = true;
        toaster.pop('wait', '提示', '订单正在提交中...');
        apiService.post({
            modelName: 'recharge',
            apiName: 'addRecharge'
        }, $scope.recharge, function (data) {
            $scope.btn_disabled = false;
            if (data.code == 200) {
                toaster.clear();
                toaster.pop('success', '提示', data['msc']);
                $scope.recharge = {
                    accountId: $rootScope.userObj['id']
                };
                window.location.href=data['data'];
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });

    }

    // 转账
    $scope.transferFun = function () {
        $scope.btn_disabled = true;
        toaster.pop('wait', '提示', '订单正在提交中...');
        apiService.post({
            modelName: 'transferAccounts',
            apiName: 'addTransferAccounts'
        }, $scope.recharge, function (data) {
            $scope.btn_disabled = false;
            if (data.code == 200) {
                toaster.clear();
                toaster.pop('success', '提示', data['msc']);
                $scope.recharge = {
                    accountId: $rootScope.userObj['id']
                };

                $timeout(function () {
                    $window.location.reload();
                }, 300);

            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });
    }


    // 提现
    $scope.desposit = {
        accountId: $rootScope.userObj?$rootScope.userObj['id']:'',
        commission:'0',
        payChannel:'2'
    };
    $scope.despositFun = function () {
        $scope.btn_disabled = true;
        toaster.pop('wait', '提示', '订单正在提交中...');
        apiService.post({
            modelName: 'withdrawDeposit',
            apiName: 'addWithdrawDeposit'
        }, $scope.desposit, function (data) {

            console.log(data);
            $scope.btn_disabled = false;
            if (data.code == 200) {
                // toaster.clear();
                toaster.pop('success', '提示', data['msc']);
                $timeout(function () {
                    $window.location.reload();
                }, 1000);
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });

    }



        //获得城市数据
        if ($rootScope.areaData) {
            return false;
        }
        $http.get('/city.json', {})
            .success(function (data, status, headers, config) {
                $rootScope.areaData = data;
            });
    
        $scope.makeCityData=function(areaName){
            $scope.cityData='';
            angular.forEach($rootScope.areaData,function(val,key){
                if(val.p==areaName){
                    $scope.cityData=val.c;
                }
            });
        }


}]);